/**
 * Created by root on 6/1/19.
 */
layui.extend({
    baseConfig : "/js/pages/baseConfig"
})
layui.use(['form','layer','laydate','table','laytpl','element','util','baseConfig'],function(){

    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        upload = layui.upload,
        layedit = layui.layedit,
        laydate = layui.laydate,
        baseConfig = layui.baseConfig,
        $ = layui.jquery;

    //监听提交
    form.on("submit(submitBtn)",function( data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        var number = data.field.number;
        //编号只能包含数字和字母
        if( !/^[0-9A-Z]*$/.test( number)){
            top.layer.msg( "合作商编号只能包含数字和大写字母");
            return;
        }
        //提交数据
        $.post("/cooperativeNumber/addOrUpdate",{
            id : data.field.id,
            jobNumber : data.field.jobNumber,
            number : data.field.number,
            name : data.field.name,
            address : data.field.address,
            siren : data.field.siren,
            phone : data.field.phone,
            taxNumber : data.field.taxNumber,
            addr : data.field.addr,
            emailCode : data.field.emailCode,
            city : data.field.city,
            country : data.field.country,
            nameRule : data.field.nameRule,
            idRule : data.field.idRule,
            codeHt20Rule : data.field.codeHt20Rule,
            codeHt10Rule : data.field.codeHt10Rule,
            codeHt5Rule : data.field.codeHt5Rule,
            codeHt2Rule : data.field.codeHt2Rule,
            codeHt0Rule: data.field.codeHt0Rule
        },function( res){
            if( res.code == 200){
                setTimeout(function(){
                    top.layer.close( index);
                    top.layer.msg( res.msg);
                    layer.closeAll( "iframe");
                    //刷新父页面
                    parent.location.reload();
                },500);
            }else{
                top.layer.close( index);
                top.layer.msg( res.msg);
            }
        });
    });
    //加载员工列表
    function loadUserSelect() {
        $.get( "/userAdmin/listForSelect",function( res) {
            users = res;
            var sel = $("select[name=jobNumber]");
            sel.append("<option value=\"请选择\">请选择</option> ");
            for (var i = 0; i < users.length; i++) {
                var user = users[i];
                if (user.jobNumber) {
                    sel.append("<option value=\"" + user.jobNumber + "\">" + user.jobNumber + "</option> ")
                }
            }
            let inputJobNumberId = $("input[name=jobNumberId]");
            let jobNumber = inputJobNumberId.val();
            //更新渲染
            sel.val(jobNumber);
            form.render("select");
        });
    }
    loadUserSelect();
});
